package ru.aptu.billing;

import ru.aptu.billing_client.BillingClient;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: andrey
 * Date: 2/11/12
 * Time: 4:33 AM
 */

public class BillingClientFactory {

    // The file is located in src/main/webapp/WEB-INF/classes
    private static final String CONFIG_FILE = "/billing_client.properties";

    public static BillingClient createDefaultBillingClient() {
        InputStream inputStream = BillingClient.class.getResourceAsStream(CONFIG_FILE);

        Properties properties = new Properties();

        try {
            properties.load(inputStream);
        } catch (IOException e) {
            System.err.println("Error reading billing client properties.");
            return null;
        }

        System.setProperty("javax.net.ssl.trustStore", "webapps/gsp_banking-0.5/WEB-INF/classes/" + properties.getProperty("key.name"));
        System.setProperty("javax.net.ssl.trustStorePassword", properties.getProperty("key.password"));

        return new BillingClient(Integer.parseInt(properties.getProperty("port")), properties.getProperty("server.address"));
    }

}
